/*
 * 文件名：BusinnessController.java
 * 版权：Copyright by www.ixuenong.com
 * 描述：
 */

package com.pointlion.sys.mvc.admin.business;

import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.pointlion.sys.mvc.admin.user.SysUserService;
import com.pointlion.sys.mvc.admin.workflow.WorkFlowService;
import com.pointlion.sys.mvc.common.base.BaseController;
import com.pointlion.sys.mvc.common.model.OaBusiness;
import com.pointlion.sys.mvc.common.model.SysOrg;
import com.pointlion.sys.mvc.common.model.SysUser;
import com.pointlion.sys.mvc.common.utils.UuidUtil;
import com.pointlion.sys.plugin.shiro.ShiroKit;

/**
 * 
 * 费用管理
 * 
 * @author clatt
 * @since 2018年12月5日
 */
public class BusinessController extends BaseController
{
    
    WorkFlowService wfservice = WorkFlowService.me;
    BusinessService service = BusinessService.me;
    SysUserService userservice = SysUserService.me;
    
    /**
     * 
     * 申请页面
     */
    public void apllyPage(){
        setBread("费用申请",this.getRequest().getServletPath(),"管理");
        render("/WEB-INF/admin/business/apply/list.html");
    }
    /**
     * 
     * 申请列表
     */
    public void applyList(){
        String curr = getPara("pageNumber");
        String pageSize = getPara("pageSize");
        String userId = ShiroKit.getUserId();
        Page<Record> page = OaBusiness.dao.getPage(Integer.valueOf(curr),Integer.valueOf(pageSize),userId);
        renderPage(page.getList(),"" ,page.getTotalRow());
    }
    /**
     * 
     * 费用申请编辑
     */
    public void applyEditPage()
    {
        String parentPath = this.getRequest().getServletPath().substring(0,this.getRequest().getServletPath().lastIndexOf("/")+1); 
        setBread("费用申请",parentPath+"apllyPage","管理");
        //添加和修改
        String id = getPara("id");//修改
        if(StrKit.notBlank(id)){//修改
            OaBusiness o = OaBusiness.dao.findById(id);
            setAttr("o", o);
            //是否是查看详情页面
            String view = getPara("view");//查看
            if("detail".equals(view)){
                setAttr("view", view);
                if(StrKit.notBlank(o.getProcInsId())){
                    setAttr("procInsId", o.getProcInsId());
                    setAttr("defId", wfservice.getDefIdByInsId(o.getProcInsId()));
                }
            }
        }else{//新增
            OaBusiness o = new OaBusiness();
            String userId = ShiroKit.getUserId();//用户主键
            SysUser user = SysUser.dao.getById(userId);//用户对象
            SysOrg org = SysOrg.dao.getById(user.getOrgid());//单位对象
            o.setSenderId(userId);
            
            o.setSenderName(user.getName());
            o.setSenderOrgid(org.getId());
            o.setSenderOrgname(org.getName());
            setAttr("o",o);
        }
        
        List<SysUser> lmUsers = userservice.getUserListByRoleId("06d4a9725aae451fb92dfddb0e8307db");
        setAttr("lmUsers",lmUsers);
        List<SysUser> bdUsers = userservice.getUserListByRoleId("98666e10545b435eb895536906d88600");            
        setAttr("bdUsers",bdUsers);
        
        render("/WEB-INF/admin/business/apply/edit.html");
    }
    /**
     * 
     * 保存
     */
    public void applySave()
    {
        OaBusiness model = getModel(OaBusiness.class, "business");
        if(StrKit.isBlank(model.getId()))
        {
            model.setId(UuidUtil.getUUID());
            model.save();
        }
        else
        {
            model.update();
        }
        renderSuccess();
    }
    /**
     * 
     * 开始流程
     */
    public void startProcess(){
        String id = getPara("id");
        service.startProcess(id);
        renderSuccess("启动成功");
    }
    
    /**
     * 
     * 待办流程
     */
    public void approvalPage(){
        setBread("费用审核",this.getRequest().getServletPath(),"待办任务");
        render("/WEB-INF/admin/business/approve/list.html");
    }
    public void approveData(){
        String curr = getPara("pageNumber");
        String pageSize = getPara("pageSize");
        Page<Record> page = wfservice.getToDoPageByKey(Integer.valueOf(curr),Integer.valueOf(pageSize),BusinessConstants.BUSINESS_TABLENAME,BusinessConstants.DEFKEY_BUSINESS, ShiroKit.getUsername());
        renderPage(page.getList(),"",page.getTotalRow());
    }
    
    
    /***
     * 取到办理页面
     */
    public void approveTask(){
        String parentPath = this.getRequest().getServletPath().substring(0,this.getRequest().getServletPath().lastIndexOf("/")+1); 
        String taskid = getPara("taskid");
        String id = getPara("id");
        OaBusiness o = OaBusiness.dao.findById(id);
        Record task = wfservice.getTaskRecord(taskid);
        setAttr("o", o);
        setAttr("task", task);
        setAttr("view", "auth");
        if(StrKit.notBlank(o.getProcInsId())){
            setAttr("procInsId", o.getProcInsId());
            setAttr("defId", wfservice.getDefIdByInsId(o.getProcInsId()));
        }
        setBread("费用申请",parentPath+"apllyPage","管理");
        
        List<SysUser> lmUsers = userservice.getUserListByRoleId("06d4a9725aae451fb92dfddb0e8307db");
        setAttr("lmUsers",lmUsers);
        List<SysUser> bdUsers = userservice.getUserListByRoleId("98666e10545b435eb895536906d88600");            
        setAttr("bdUsers",bdUsers);
        
        render("/WEB-INF/admin/business/apply/edit.html");
    }
    
    /***
     * 办理任务
     */
    public void businessSubmit(){
        try{
            String comment = getPara("comment");
            service.completeTask(getParaToBoolean("pass"),comment,getPara("taskid"), getModel(OaBusiness.class,"business"));
            renderSuccess("提交成功");
        }catch(Exception e){
            e.printStackTrace();
            renderError("提交失败");
        }
    }
    
    
    /***
     * 历史数据页面
     */
    public void historyPage(){
        setBread("费用审核",this.getRequest().getServletPath(),"费用历史");
        render("/WEB-INF/admin/business/history/list.html");
    }
    
    /***
     * 所有历史数据
     */
    public void historyData(){
        String curr = getPara("pageNumber");
        String pageSize = getPara("pageSize");
        Page<Record> page = service.getPartList(Integer.valueOf(curr),Integer.valueOf(pageSize), null, ShiroKit.getUserId());
        renderPage(page.getList(),"" ,page.getTotalRow());
    }
    
    /***
     * 删除
     * @throws Exception
     */
    public void delete() throws Exception{
		String ids = getPara("ids");
    	//执行删除
		service.deleteByIds(ids);
    	renderSuccess("删除成功!");
    }
}
